<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_services extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('model_services', 'm_serv', TRUE);
        $this->load->model('model_state', 'm_state', TRUE);
    }
    
    function index() {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            
            $data = array(
                'web_title' =>  'Admin Services',
                'main_view' =>  'mimin/services/index_new',
                'list_data' =>  $this->m_serv->get_all(),
            );
            $this->load->view('mimin/template/template_table',$data);
        }
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        else{
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('phone','Telp','required');
            $this->form_validation->set_rules('address','Alamat','required');
            $this->form_validation->set_rules('province','ID Provinsi','required');
            $this->form_validation->set_rules('city','ID Kota','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'     =>  'Data Services Baru',
                    'main_view'     =>  "mimin/services/add",
                    "list_state"    =>  $this->m_state->get_all(),
                );
                $this->load->view("mimin/template/template_editor_map",$data);
//                $this->load->view("mimin/test/test");
            }
        }
    }
    /*
     * author dinov
     * 20141017
     * form add services action
     */
    function add_act(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('phone','Telp','required');
            $this->form_validation->set_rules('address','Alamat','required');
            $this->form_validation->set_rules('city','Kota','required');
            $this->form_validation->set_rules('desc','Keterangan','required');
            $this->form_validation->set_rules('lat','Keterangan','required');
            $this->form_validation->set_rules('long','Keterangan','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "All fields must be filled",
                );
                echo json_encode($output);exit();
            }
                
                //SET TIME ZONE
                date_default_timezone_set("Asia/Jakarta");
                $curdate = date("Y-m-d h:i:s");

                $isi_new = array(
                    'name'          =>  $this->input->post('name'),
                    'phone'         =>  $this->input->post('phone'),
                    'address'       =>  $this->input->post('address'),
                    'description'   =>  $this->input->post('desc'),
                    'city_id'       =>  $this->input->post('city'),
                    'lat'           =>  $this->input->post('lat'),
                    'long'          =>  $this->input->post('long'),
                    'datecreated'   =>  $curdate,
                    'createdby'     =>  $this->session->userdata('admin_user')->id_admin,
                );
                $status_save = $this->m_serv->save_data($isi_new);
                if($status_save){
                    $output = array(
                        'status'    =>  1,
                        'msg'       =>  " New Data has been inserted",
                    );
                    echo json_encode($output);exit();
                }
                else{
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Save data failed, ERROR : ".  mysql_error(),
                    );
                    echo json_encode($output);exit();
                }
        }
        else{
            die("cannot access directly");
        }
    }
    /*
     * author dinov
     * 20141017
     * get city list based on selected state id
     */
    function get_city_list(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            $this->form_validation->set_rules('state_id','ID','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "State ID is Missed",
                );
                echo json_encode($output);exit();
            }
            $condition = array(
                'city_state_id' =>  $this->input->post('state_id')
            );
            $list_city = $this->m_serv->get_city_list($condition);
            $output_list = "";
            if($list_city->num_rows()>0){
                $output_list.="<select id='city' name='city' class='form-control'>";
                foreach ($list_city->result() as $r):
                    $output_list.="<option value='".$r->city_id."'>".$r->city_name."</option>";
                endforeach;
                $output_list.="</select>";
            }
            else{
                $output_list.="<select id='city'><option value=''> -Data kota tidak tersedia-</option></select>";
            }
            
            $output = array(
                'status'    =>  1,
                'msg'       =>  "",
                'list_city' =>  $output_list
            );
            echo json_encode($output);exit();
        }
        else{
            die("cannot access directly");
        }
    }
    
    /*
     * author dinov
     * 20141020
     * delete services data action 
     * parameter : id  required from $_POST
     */
    function delete(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            if(!$this->session->userdata('admin_user')){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "You need to re-login"
                );
                echo json_encode($output);exit();
            }
            if($this->session->userdata('admin_user')->level != 1){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "You're not allowed to run this function"
                );
                echo json_encode($output);exit();
            }
            $this->form_validation->set_rules('id','id services','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "ID is missed"
                );
                echo json_encode($output);exit();
            }
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            $count = $this->m_serv->select_by_id($this->input->post('id'),"count");
            if($count > 0){
                $list_data = $this->m_serv->select_by_id($this->input->post('id'),"result");

                $this->db->trans_begin();
                //delete permanently

                $status_delete = $this->m_serv->delete($this->input->post('id'));
                if($status_delete && $this->db->trans_status() === TRUE){
                    $this->db->trans_commit();
                    $output = array(
                        'status'    =>  1,
                        'msg'       =>  "success Delete Services data permanently, page will be refreshed in short times"
                    );
                    echo json_encode($output);exit();
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "data deletion has failed, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
        else{
            die("cannot access directly");
        }
    }
    
    
    function detail($id=""){
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d h:i:s");
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        elseif($id==""){
            redirect("admin_services","refresh");
        }
        else{
            $count = $this->m_serv->select_by_id($id,"count");
            
            if($count > 0){
                $list_data = $this->m_serv->select_by_id($id);
                $state_id="";
                foreach ($list_data as $l){
                    $state_id = $l->city_state_id;
                }
                //get city list
                $condition = array(
                    'city_state_id' =>  $state_id
                );
                $data = array(
                    "main_view"     =>  "mimin/services/detail",
                    "web_title"     =>  "Detail Services Data",
                    "list_data"     =>  $this->m_serv->select_by_id($id),
                    "list_state"    =>  $this->m_state->get_all(),
                    "list_city"     =>  $this->m_serv->get_city_list($condition),
                );
                $this->load->view("mimin/template/template_editor_map",$data);
            }
            else{
                redirect("admin_services","refresh");
            }
        }
    }
    
    /*
     * author dinov
     * 20141020
     * form update service validation
     */
    function detail_act(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            $this->form_validation->set_rules('id_services','ID','required');
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('phone','Telp','required');
            $this->form_validation->set_rules('address','Alamat','required');
            $this->form_validation->set_rules('city','Kota','required');
            $this->form_validation->set_rules('desc','Keterangan','required');
            $this->form_validation->set_rules('lat','Keterangan','required');
            $this->form_validation->set_rules('long','Keterangan','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "All fields must be filled",
                );
                echo json_encode($output);exit();
            }
            
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");

            $isi_update = array(
                'name'          =>  $this->input->post('name'),
                'phone'         =>  $this->input->post('phone'),
                'address'       =>  $this->input->post('address'),
                'description'   =>  $this->input->post('desc'),
                'city_id'       =>  $this->input->post('city'),
                'lat'           =>  $this->input->post('lat'),
                'long'          =>  $this->input->post('long'),
                'datemodified'  =>  $curdate,
                'modifiedby'    =>  $this->session->userdata('admin_user')->id_admin,
            );
            $status_update = $this->m_serv->update_data($this->input->post('id_services'),$isi_update);
            if($status_update){
                $output = array(
                    'status'    =>  1,
                    'msg'       =>  "Data has been updated",
                );
                echo json_encode($output);exit();
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Save data failed, ERROR : ".  mysql_error(),
                );
                echo json_encode($output);exit();
            }
        }
        else{
            die("Cannot access directly");
        }
    }
}

/* End of file admin_services.php */
/* Location: ./application/controllers/admin_services.php */